/////////////////////////////////////////////////
repo:    cdriven6x
target:  comment_driven
branch:  6.x-1.x
tags:    
node:    b9603d9b669d1dc5b682c3a5faa59361de832ae1
changes:
* _comment_driven_setup_proxy_render to address #pre_render, #theme, #post_render (factorized)
* pass through the same arguments we got
/////////////////////////////////////////////////
=================================================
branch:  6.x-1.x
tags:    tip
node:    c51a638486d9800cdf41c05d11fc0c620e1a7991
=================================================
diff -r b9603d9b669d -r c51a638486d9 comment_driven/comment_driven.module
--- comment_driven.module	Mon Apr 12 23:20:58 2010 -0500
+++ comment_driven.module	Thu Apr 15 09:32:28 2010 -0500
@@ -83,6 +83,17 @@ function comment_driven_node_type($op, $
   }
 }
 
+/**
+ * Implements hook_theme().
+ */
+function comment_driven_theme() {
+  return array(
+    'comment_driven_proxy' => array(
+      'arguments' => array('element' => NULL),
+    ),
+  );
+}
+
 ///\\\///\\\///\\\///\\\///
 
 /**
@@ -191,7 +202,10 @@ function comment_driven_is_live_render($
 
 function _comment_driven_disguise_children($element) {
   // @TODO: every call to comment_driven_assertion_failed will be removed after alpha/beta stage 
-  if (!is_array($element)) comment_driven_assertion_failed('something nasty is happening somewhere: !is_array($element)');
+  if (!is_array($element)) {
+    comment_driven_assertion_failed('something nasty is happening somewhere: !is_array($element)');
+    return;
+  }
   
   // adjust #parents to match the disguised elements of comment_form
   // note that no parent would point to a level above the comment_form
@@ -208,6 +222,7 @@ function _comment_driven_disguise_childr
     // protect ourself against 3rd parties creating NULL children (e.g. [#736968], [#737044])
     // lets babysit NULL children, but it has to be an array otherwise
     if (!is_null($element[$key])) {
+      _comment_driven_setup_proxy_render($element[$key]);
       $element[COMMENT_DRIVEN__DISGUISE_PREFIX . $key] = _comment_driven_disguise_children($element[$key]);
     }
     // if it was NULL (above babysitting) won't hurt clearing it
@@ -226,6 +241,58 @@ function _comment_driven_disguise_adjust
   }
 }
 
+function _comment_driven_setup_proxy_render(&$element) {
+  $proxy_render = array(
+    '#pre_render' => '_comment_driven_pre_render_proxy', 
+    '#theme' => 'comment_driven_proxy', // points to theme_comment_driven_proxy
+    '#post_render' => '_comment_driven_post_render_proxy',
+  );
+  foreach ($proxy_render as $key => $proxy) {
+    if (isset($element[$key])) {
+      // disguise_children supports only element_children
+      // (disguise_form_values doesn't address non-children either) 
+      // undisguise does support non-children as well
+      // manually disguise non-children
+      // @TODO: maybe it is time to have non-children support into disguise_children (?)
+      $element['#' . COMMENT_DRIVEN__DISGUISE_PREFIX . $key] = $element[$key];
+      $element[$key] = $proxy;
+    }
+  }
+}
+
+// $args not used when $key is '#theme'
+function _comment_driven_proxy_render($element, $key, $args) {
+  // $key is expected to be overriden by its disguised version
+  // but, just in case, lets ensure we won't fall back here (unintended recursion) 
+  unset($element[$key]);
+  $element = _comment_driven_undisguise($element);
+  if ($key === '#theme') {
+    // if we got here, then #theme function/template is expected to exists
+    return theme($element['#theme'], $element);
+  }
+  $function = $element[$key];
+  // if we got here, then target function is expected to exists
+  return call_user_func_array($function, $args);
+}
+
+// proxy_render
+function theme_comment_driven_proxy($element) {
+  $args = func_get_args(); // pass through the same arguments we got
+  return _comment_driven_proxy_render($element, '#theme', $args);
+}
+
+// proxy_render
+function _comment_driven_pre_render_proxy($element) {
+  $args = func_get_args(); // pass through the same arguments we got
+  _comment_driven_proxy_render($element, '#pre_render', $args);
+}
+
+// proxy_render
+function _comment_driven_post_render_proxy($content, $element) {
+  $args = func_get_args(); // pass through the same arguments we got
+  _comment_driven_proxy_render($element, '#post_render', $args);
+}
+
 // helper function to drive properties programmatically [#741274] 
 function comment_driven_disguise_form_values($arr) {
   // @TODO: every call to comment_driven_assertion_failed will be removed after alpha/beta stage 
/////////////////////////////////////////////////
generated with: drudiff cdriven6x comment_driven 173 180 
/////////////////////////////////////////////////
